﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FZ.Common;
using TongJi.Model;

namespace TongJi.Commmon
{
    public class UtilHelper
    {
        private static readonly string AnalyzeInfoConfigPath = "~/config/BrowserAnalyze.config".ToMapPath();
        private static List<BrowserAnalyzeInfo> GetAnalyzeInfoConfig()
        {
            var data = CacheHelper.GetCache(AnalyzeInfoConfigPath) as List<BrowserAnalyzeInfo>;
            if (data == null)
            {
                if (!File.Exists(AnalyzeInfoConfigPath))
                {
                    File.AppendAllText(AnalyzeInfoConfigPath, XmlHelper.Serializer(new List<BrowserAnalyzeInfo>()
                    {
                        new BrowserAnalyzeInfo()
                        {
                            BrowserName = "IE",
                            BrowserRegex = @"msie ([\d.]+)",
                            VersionRegexIndex = 1,
                            Order = 100,
                            BroswerAgentName = "test",
                            VersionAgentRegexIndex = 1
                        }
                    }));
                }
                var content = File.ReadAllText(AnalyzeInfoConfigPath);
                if (content.IsNullOrWhiteSpace())
                {
                    throw new Exception("{0}内容为空，请配置".FormatWith(AnalyzeInfoConfigPath));
                }
                data = XmlHelper.Deserialize<List<BrowserAnalyzeInfo>>(content);
                if (data == null)
                {
                    throw new Exception("{0}配置错误，请检查".FormatWith(AnalyzeInfoConfigPath));
                }
                data = data.OrderBy(p => p.Order).ToList();
                CacheHelper.SetCache(AnalyzeInfoConfigPath, data, AnalyzeInfoConfigPath);
            }
            return data;
        }
        /// <summary>
        /// 返回浏览器信息 
        /// </summary>
        /// <param name="userAgent"></param>
        /// <returns></returns>
        public static BrowserInfo GetBrowserInfo(string userAgent)
        {
            var config = GetAnalyzeInfoConfig();

            foreach (var item in config)
            {
                var t = Regex.Match(userAgent, item.BrowserRegex, RegexOptions.IgnoreCase);
                if (t.Success)
                {
                    var d = new BrowserInfo()
                    {
                        BrowserName = item.BrowserName,
                        BrowserVersion = t.Groups[item.VersionRegexIndex].Value
                    };
                    d.BrowserAgentName = item.BroswerAgentName;
                    if (item.VersionAgentRegexIndex.HasValue)
                    {
                        d.BrowserAgentVersion = t.Groups[item.VersionAgentRegexIndex.Value].Value;
                    }
                    return d;
                }
            }
            return new BrowserInfo();
        }
    }
}
